<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/12/26
 * Time: 14:19
 */

class SocietyModel
{
    public function myPostsAction($user)
    {
        $data['lists'] = Db::name('society')
            ->field('id as sid,title')
            ->where(['fid'=>$user['fid'],'udid'=>$user['id'],'status'=>1])
            ->order('id desc')
            ->select();
        $data['count'] = Db::name('society')->where(['fid'=>$user['fid'],'udid'=>$user['id'],'status'=>1])->count();
        return $data;
    }

    public function listsAction($fid,$page,$number=PAGE_NUMBER)
    {
        $data['lists'] = Db::name('society')
            ->field('s.id as sid,s.title,left(s.content,96) as content,s.images,s.comment,s.createtime,ud.nickname,ud.avatar')->alias('s')
            ->join('users_detail ud','s.udid = ud.id')
            ->where(['s.fid'=>$fid,'s.status'=>1])
            ->page($page,$number)
            ->order('s.id desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('society')->where(['fid'=>$fid,'status'=>1])->count();
        return $data;
    }

    public function detailedAction($fid,$sid,$number=PAGE_NUMBER)
    {
        $data['society'] = Db::name('society')
            ->field('s.id as sid,s.title,s.images,s.content,s.createtime,s.comment,ud.nickname,ud.avatar,ud.section')->alias('s')
            ->join('users_detail ud','s.udid = ud.id')
            ->where(['s.id'=>$sid,'s.fid'=>$fid,'s.status'=>1])
            ->find();
        $data['comment'] = Db::name('comment')
            ->field('c.content,c.createtime,ud.nickname,ud.avatar')->alias('c')
            ->join('users_detail ud','c.udid = ud.id')
            ->where(['c.sid'=>$sid,'c.fid'=>$fid,'c.status'=>1])
            ->order('c.id desc')
            ->page(1,$number)
            ->select();
        $data['page'] = 1;
        $data['count'] = Db::name('comment')->where(['sid'=>$sid,'fid'=>$fid,'status'=>1])->count();
        return $data;
    }

    public function commentAction($fid,$content,$number=PAGE_NUMBER)
    {
        $data['lists'] = Db::name('comment')
            ->field('c.content,c.createtime,ud.nickname,ud.avatar')->alias('c')
            ->join('users_detail ud','c.udid = ud.id')
            ->where(['c.sid'=>$content['sid'],'c.fid'=>$fid,'c.status'=>1])
            ->order('c.id desc')
            ->page($content['page'],$number)
            ->select();
        $data['page'] = $content['page'];
        $data['count'] = Db::name('comment')->where(['sid'=>$content['sid'],'fid'=>$fid,'status'=>1])->count();
        return $data;
    }

    public function commentPostAction($user,$content)
    {
        $data['sid'] = $content['sid'];
        $data['content'] = $content['content'];
        $data['udid'] = $user['id'];
        $data['firm'] = $user['firm'];
        $data['fid'] = $user['fid'];
        $data['status'] = 1;
        Db::name('comment')->insert($data);
        Db::name('society')->where(['id'=>$data['sid'],'fid'=>$user['fid']])->setInc('comment');
        $msg['message'] = '评论发布成功';
        return $msg;
    }

    public function sendPostAction($user,$content)
    {
        $data['udid'] = $user['id'];
        $data['firm'] = $user['firm'];
        $data['fid'] = $user['fid'];
        $data['title'] = $content['title'];
        $data['content'] = $content['content'];
        $data['status'] = 1;
        $path = $content['path'];
        $file = $content['file'];
        unset($content['path']);
        unset($content['file']);
        if($path && $file){
            $path = explode(',',$path);
            $file = explode(',',$file);
            if(count($path) && count($file)){
                $filePath = [];
                $model = new ModelModel();
                foreach ($path as $k => $v){
                    $filePath[$k] = $model->moveImg($v,$file[$k],2);
                }
                $data['images'] = implode(',',$filePath);
            }
        }
        Db::name('society')->insert($data);
        $msg['message'] = '帖子发布成功';
        return $msg;
    }

    public function postsAction($user,$page,$number=PAGE_NUMBER)
    {
        $data['lists'] = Db::name('society')
            ->field('s.id as sid,s.title,left(s.content,96) as content,s.images,s.comment,s.createtime,ud.nickname,ud.avatar')->alias('s')
            ->join('users_detail ud','s.udid = ud.id')
            ->where(['s.fid'=>$user['fid'],'s.udid'=>$user['id'],'s.status'=>1])
            ->page($page,$number)
            ->order('s.id desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('society')->where(['fid'=>$user['fid'],'udid'=>$user['id'],'status'=>1])->count();
        return $data;
    }
}